﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiagramDesigner;
using System.Windows.Input;

namespace DemoApp
{
    public class ComputationUnitViewModel : DesignerItemViewModelBase, ISupportDataChanges
    {
        private IUIVisualizerService visualiserService;

        public ComputationUnitViewModel(int id, DiagramViewModel parent, double left, double top, 
            Dictionary<int, string> language, List<string> methodNames, List<string> methodsCode)
            : base(id, parent, left, top)
        {
            this.Language = language;
            this.MethodNames = methodNames;
            this.MethodCodes = methodsCode;
            generateMethodText();
            Init();
        }

        public ComputationUnitViewModel()
            : base()
        {
            Language = new Dictionary<int, string>();
            MethodNames = new List<string>();
            MethodCodes = new List<string>();
            Init();
        }

        public Dictionary<int, string> Language { get; set; }
        public List<string> MethodNames { get; set; }
        public List<string> MethodCodes { get; set; }
        public ICommand ShowDataChangeWindowCommand { get; private set; }
        public String StrMethodNames { get; set; }

        public int GetLanguageId()
        {
            int langid = 1;
            foreach (int key in Language.Keys)
                langid = key;
            return langid;
        }

        public string GetLanguageName()
        {
            string langname = "Java";
            foreach (string value in Language.Values)
                langname = value;
            return langname;
        }

        public void ExecuteShowDataChangeWindowCommand(object parameter)
        {
            //ComputationUnitData data = new ComputationUnitData("", "");
            Dictionary<int, string> lang = Language;
            List<string> mNames = MethodNames;
            List<string> mCodes = MethodCodes;
            if (visualiserService.ShowEditDialog(ref lang, ref mNames, ref mCodes) == true)
            {
                //this.MethodNames.Add(data.MethodName);
                //this.MethodCodes.Add(data.MethodCode);
                generateMethodText();
            }
        }

        private void Init()
        {
            visualiserService = ApplicationServicesProvider.Instance.Provider.VisualizerService;
            ShowDataChangeWindowCommand = new SimpleCommand(ExecuteShowDataChangeWindowCommand);
            this.ShowConnectors = false;
        }

        private void generateMethodText()
        {
            this.StrMethodNames = "";
            //ApplicationServicesProvider.Instance.Provider.MessageBoxService.ShowInformation(this.MethodNames.Count.ToString());
            //foreach (var methodName in this.MethodNames)
            //{
            //    if (!String.IsNullOrEmpty(this.StrMethodNames))
            //        this.StrMethodNames += "\n";
            //    this.StrMethodNames += methodName;
            //}
            if (this.MethodNames.Count > 1)
                this.StrMethodNames = this.MethodNames.Count + " Methods";
            else if(this.MethodNames.Count == 1)
                this.StrMethodNames = this.MethodNames[0];
            NotifyChanged("StrMethodNames");
        }
    }
}
